from django.http import HttpResponseRedirect
from django.db.models import Q
from django.shortcuts import render_to_response
from books.models import Book
from django.core.mail import send_mail
from forms import ContactForm
from forms import PublisherForm

from forms import PublisherForm

def add_publisher(request):
    if request.method == 'POST':
        form = PublisherForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/add_publisher/thanks/')
    else:
        form = PublisherForm()
    return render_to_response('books/add_publisher.html', {'form': form})


def search(request):
    query = request.GET.get('q', '')
    if query:
        qset = (
            Q(title__icontains=query) |
            Q(authors__first_name__icontains=query) |
            Q(authors__last_name__icontains=query)
        )
        results = Book.objects.filter(qset).distinct()
    else:
        results = []
    return render_to_response("search.html", {
        "results": results,
        "query": query
    })

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            topic = form.cleaned_data['topic']
            message = form.cleaned_data['message']
            sender = form.cleaned_data.get('sender','noreply@examplse.com')
            send_mail('Feedback from your site, topic: %s'%topic,
                      message, sender,
                      ['wwq0327@gmail.com']
                      )
            return HttpResponseRedirect('/contact/thanks')
    else:
        form = ContactForm()
        
    return render_to_response('contact.html', {'form':form})
